/*
 * Copyright (c) 2012, Marcin Czekaj
 * All rights reserved.
 *
 * This software is open-source under the BSD license; see LICENSE.TXT for a description.
 */
package pl.edu.uj.fais.gui;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.WindowEvent;
import java.awt.geom.Point2D;
import java.io.File;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import pl.edu.uj.fais.adapters.WindowListenerAdapter;
import pl.edu.uj.fais.controller.Controller;

public class MainWindow extends JFrame {
    
    private static final long serialVersionUID = 8242530624291595536L;
    
    private static final String WINDOW_TITLE = "Graph Editor";
    private static final int WINDOW_WIDTH = 600;
    private static final int WINDOW_HEIGHT = 500;
    
    private MainMenu mainMenu;
    
    public MainWindow() {
        
        initLookAndFeel();
        initWindow();
        initController();
        
        addWindowListener(new WindowListenerAdapter() {
        	@Override
        	public void windowClosing(WindowEvent e) {
        		Controller.getInstance().exit();
        	}
		});
        
        setVisible(true);
    }
    
    public void setEmptyWindowTitle() {
        setTitle(WINDOW_TITLE);
    }
    
    public void setUntitledWindowTitle() {
        setWindowTitle(null);
    }
    
    public void setWindowTitle(File file) {
        String filename = file != null ? file.getName() : "untitled";
        setTitle(String.format("%s - %s", WINDOW_TITLE, filename));
    }
    
    public Dimension getClientSize() {
        return getClientSize(0);
    }
    
    public Dimension getClientSize(int padding) {
        
        int width = getWidth() - padding;
        int height = getHeight() - padding;
        
        return new Dimension(width, height);
    }
    
    public Point2D getCenter() {
        Rectangle bounds = getBounds();
        return new Point2D.Double(bounds.width / 2.0, bounds.height / 2.0);
    }
    
    public void showErrorMessage(String message) {
        JOptionPane.showMessageDialog(this,
                                      message,
                                      "Error",
                                      JOptionPane.ERROR_MESSAGE);
    }
    
    private void initLookAndFeel() {
        // @formatter:off
        String className = UIManager.getSystemLookAndFeelClassName();
        
        try {
            UIManager.setLookAndFeel(className);
        } catch (ClassNotFoundException |
                 InstantiationException |
                 IllegalAccessException |
                 UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }
        // @formatter:on
    }
    
    private void initWindow() {
        setJMenuBar(mainMenu = new MainMenu());
//        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setEmptyWindowTitle();
        setLocationRelativeTo(null);
    }
    
    private void initController() {
        Controller controller = Controller.getInstance();
        controller.setMainWindow(this);
        controller.setMainMenu(mainMenu);
    }
}
